<template>
   <div class="login_container">
      <div class="login_box">
         <!-- 头像 -->
         <div class="logo_box">
            <img src="../assets/logo.png" >
         </div>
         <!-- 表单 -->
         <el-form ref="loginForm" :rules="rules" :model="loginForm" class="login_form">
            <el-form-item prop="username">
               <el-input v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password" >
               <el-input type="password" v-model="loginForm.password"></el-input>
            </el-form-item>
            <!-- 按钮 -->
            <el-form-item class="btns">
               <el-button type="primary" @click="login">登录</el-button>
               <el-button type="info">重置</el-button>
            </el-form-item>
         </el-form>
      </div>
   </div>

   <!-- <div>
      <h1>登录页</h1>
      用户名 <input type="text" v-model="loginForm.username" />
      密码 <input type="password" v-model="loginForm.password" />
      <button @click="login">登录</button>
   </div> -->
</template>

<script>
export default {
   data(){
       return {
            loginForm:{
                username: 'admin',
                password: '123456'
            },
            rules: {
               username: [
                  { required: true, message: '请输入用户名', trigger: 'blur' }
               ],
               password: [
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { min: 6, max: 8, message: '长度在 6 到 8 个字符', trigger: 'blur' }
               ]
            }
       }
   },
   methods:{
         login(){
            this.$refs.loginForm.validate(async (ok)=>{
                 if(ok==false){ // 验证失败
                   this.$message.error('验证失败,请修改');
                   return ;
                 }
                 let ret = await this.http.post('login',this.loginForm)
                 
                 if(ret.meta.status != 200){
                     this.$message.error('登录失败');
                     return ;
                 }
                 //保存token 
                 sessionStorage.setItem('token',ret.data.token)
                 // 成功去首页
                 this.$router.push('/home')
            });
        }
   }
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .logo_box{
        height: 130px;
        width: 130px;
        border:1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position:absolute;
        left:50%;
        background-color: #fff;
        transform: translate(-50%, -50%);
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}

.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content: flex-end;
}
</style>